package net.lab1024.sa.base.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.SimpleTimeZone;
import java.util.TimeZone;


/**
 * @date 2018/03
 * @author szy
 * 
 */
public class DateUtils {

	/**
	 * Date Style
	 */
	public static final String DATESTYLE = "yyyyMMddHHmmss";

	/**
	 * Date Style for translation
	 */
	public static final String DATESTYLETRANS = "yyMMddHHmmss";

	/**
	 * /** Date Style for Extention
	 */
	public static final String DATESTYLE_EX = "yyyy-MM-dd_HH-mm-ss";

	/**
	 * Date Style for Year & Month
	 */
	public static final String DATESTYLE_YEAR_MONTH = "yyyyMM";

	/**
	 * Date Style for Year & Month
	 */
	public static final String DATESTYLE_YEAR_MONTH_EX = "yyyy/MM";

	public static final String DATESTYLE_YEAR_MONTH_EXF = "yyyy-M";

	/**
	 * Date Style for Short
	 */
	public static final String DATESTYLE_SHORT = "yyyyMMdd";

	/**
	 * Date Style for Short
	 */
	public static final String DATESTYLE_SHORT_YEAR = "yy/MM/dd";

	/**
	 * Date Style for detail
	 */
	public static final String DATESTYLE_DETAIL = "yyyyMMddHHmmssSSS";

	/**
	 * Date Style for material file upload
	 */
	public static final String DATESTYLE_MATERIAL_FILE_UPLOAD = "yyyyMMdd_HHmmss_SSS";

	/**
	 * Date Style for detail
	 */
	public static final String DATESTYLE_TSV_FORMAT = "yyyy-MM-dd HH:mm:ss";

	/**
	 * Date Style for detail
	 */
	public static final String DATESTYLE_TSV_FORMAT_SHORT = "yyyy-MM-dd";

	/**
	 * Date Style for detail
	 */
	public static final String DATESTYLE_COMMON = "yyyy/MM/dd";

	/**
	 * Date Style for detail
	 */
	public static final String DATESTYLE_SHORT_TIME = "yyyy/MM/dd HH:mm";

	/**
	 * Date Style for detail
	 */
	public static final String DATESTYLE_LONG_TIME = "yyyy/MM/dd HH:mm:ss";

	public static final String CUR_DATE_SQL = "getDbCurDate";

	public static final String DATESTYLE_DTEAIL = " HH:mm:ss";

	public static final String DATESTYLE_DTEAIL_HMS = "HH:mm:ss";

	/**
	 * Date Style for Short
	 */
	public static final String DATESTYLE_YEAR_POINT = "yyyy.MM.dd";

	/**
	 * Date Style for Short
	 */
	public static final String CHINESE_MONTH_DAY = "MM月dd日";

	public static final String CHINESE_MONTH = "yyyy-MM";

	public static final String CHINESE_MONTH_ZH = "yyyy年M月";

	public static final String DATESTYLE_SHORT_TIME_ZH = "yyyy年MM月dd日 HH:mm";
	/**
	 * 取当前时间
	 * 
	 * @return
	 */
	public static Date getCurDate() {
		Date date = new Date();
		return date;
	}

	/**
	 * 取当前时间
	 *
	 * @return
	 */
	public static LocalDateTime getCurLocalDate() {
		LocalDateTime date = LocalDateTime.now();
		return date;
	}

	/**
	 * 增加月数
	 *
	 * @param fromDate
	 * @param style
	 * @param addMonth
	 * @return
	 */
	public static String addMonth(Date fromDate, String style, int addMonth) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(fromDate);
		cal.add(Calendar.MONTH, addMonth);
		return (new SimpleDateFormat(style)).format(cal.getTime());
	}

	/**
	 * 增加月数
	 *
	 * @param fromDate
	 * @param style
	 * @param addMonth
	 * @return
	 */
	public static Date addMonth(Date fromDate, int addMonth) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(fromDate);
		cal.add(Calendar.MONTH, addMonth);
		return cal.getTime();
	}

	/**
	 * 根据时间样式输出时间字符串
	 *
	 * @param style
	 *            时间
	 * @return
	 */
	public static String getCurrentDate(String style) {
		SimpleDateFormat dFmt = new SimpleDateFormat(style);
		return dFmt.format(new Date());
	}


	/**
	 * 增加年
	 *
	 * @param fromDate
	 * @param style
	 * @param addDate
	 * @return
	 */
	public static String addYear(Date fromDate, String style, int addYear) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(fromDate);
		cal.add(Calendar.YEAR, addYear);
		return (new SimpleDateFormat(style)).format(cal.getTime());
	}

	/**
	 * 增加天
	 * 
	 * @param fromDate
	 * @param style
	 * @param addDate
	 * @return
	 */
	public static String addDate(Date fromDate, String style, int addDate) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(fromDate);
		cal.add(Calendar.DATE, addDate);
		return (new SimpleDateFormat(style)).format(cal.getTime());
	}

	/**
	 * 增加小时
	 * 
	 * @param fromDate
	 * @param addHour
	 * @return
	 */
	public static Date addHour(Date fromDate, int addHour) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(fromDate);
		cal.add(Calendar.HOUR, addHour);
		return cal.getTime();
	}



	/**
	 * date 转String
	 * 
	 * @param date
	 * @param style
	 * @return
	 */
	public static String dateToString(Date date, String style) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat dFmt = new SimpleDateFormat(style);
		return dFmt.format(date);
	}



	/**
	 * 日付比較 YYYY/MM/dd
	 * @param DATE1
	 * @param DATE2
	 * @param format
	 * @return
	 * @throws Exception
	 */
	public static int compareDate(String DATE1, String DATE2, String format)
			throws Exception {

		DateFormat df = new SimpleDateFormat(format);

		Date dt1 = df.parse(DATE1);

		Date dt2 = df.parse(DATE2);

		if (dt1.getTime() > dt2.getTime()) {

			// dt1 比dt2早
			return 1;

		} else if (dt1.getTime() < dt2.getTime()) {

			// dt1はdt2より遅い;

			return -1;

		} else {

			return 0;

		}

	}

	/**
	 * UTC的时间获得
	 * 
	 * @param type
	 * @return
	 */
	public static String getUtcTime(String type) {
		Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
		DateFormat df = new SimpleDateFormat(type);
		df.setTimeZone(cal.getTimeZone());
		String timestamp = df.format(cal.getTime());
		return timestamp;
	}

	/**
	 * 获得时间戳
	 *
	 * @param timeZone
	 * @param type
	 * @return
	 */

	public static String getDstTime(String timeZone, String type) {
		Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(timeZone));
		DateFormat df = new SimpleDateFormat(type);
		df.setTimeZone(cal.getTimeZone());
		String timestamp = df.format(cal.getTime());
		return timestamp;
	}


	/**
	 * 得到时间的小时 - 分钟 - 秒
	 * @param totalTime 秒
	 * @return
	 */
	public static String getTimeStr(int totalTime) {

		String totalTimeStr = "";

		if (totalTime/3600 != 0){
			totalTimeStr += totalTime/3600 + "时";
		}
		if((totalTime%3600)/60 !=0 ){
			totalTimeStr += (totalTime%3600)/60 + "分";
		}

		totalTimeStr += (totalTime%3600)%60 + "秒";

		return totalTimeStr;
	}

	/**
	 * 得到时间的分钟 - 秒
	 * @param totalTime 秒
	 * @return
	 */
	public static String getMinuteTimeStr(int totalTime) {

		String totalTimeStr = "";

		if(totalTime/60 != 0 ){
			totalTimeStr += totalTime/60 + "分";
		}

		totalTimeStr += totalTime%60 + "秒";

		return totalTimeStr;
	}



	/**
	 * 得到时间的小时 - 分钟
	 * @param totalTime 秒
	 * @return
	 */
	public static String getTimeStrCut(int totalTime) {

		String totalTimeStr = "";

		if (totalTime/3600 != 0){
			totalTimeStr += totalTime/3600 + "时";
		}
		if((totalTime%3600)/60 !=0 ){
			totalTimeStr += (totalTime%3600)/60 + "分";
		}

		return totalTimeStr;
	}

	/**
	 * 时间比较
	 * @param startTimestamp 开始时间
	 * @param endTimestamp   结束时间
	 * @param studyTime      比较时间
	 * @param cut			 误差时间
	 * @return
	 */
	public static boolean timeCompare(long startTimestamp ,long endTimestamp,Long studyTime,long cut){

		if(startTimestamp <= endTimestamp){
			long cuttime =  (endTimestamp - startTimestamp) / 1000;//观看的时间,单位是s

			if(cuttime >= studyTime){
				//正常情况下 结算时间是大于等于视频总时长的
				// 1.存在系统等待误差 2.视频暂停等待时间
				return  true;
			}else{
				//小于的情况基本是快进,不过考虑误差问题,2min以内都算通过
				if((studyTime - cuttime) < cut){
					return  true;
				}
			}
		}else{
			return  false;
		}

		return  false;
	}

	/**
	 * mongo 日期查询isodate
	 * @return
	 */
	public static Date dateToISODate(Date date){
		//T代表后面跟着时间，Z代表UTC统一时间
		SimpleDateFormat format =
				new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
		format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
		String isoDate = format.format(date);
		try {
			return format.parse(isoDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}






	/**
	 * 获取中文年 /MM月/日
	 * @return
	 */
	public static String getChineseYear(){
		String lastdaystr = "";
		try {
			//月的最后一天
			String md = DateUtils.getCurrentDate(DateUtils.CHINESE_MONTH_DAY);

			Calendar cal = Calendar.getInstance();
			String year = String.valueOf(cal.get(Calendar.YEAR));
			StringBuffer s = new StringBuffer();

			for (int i=0;i<year.length();i++)
			{
				s.append(formatDigit(year.charAt(i)));
			}

			return s.toString() +'年'+ md;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lastdaystr;
	}


	public static String formatDigit(char sign) {
		String var = "";
		switch (sign) {
			case '0':
				var = "〇";
				break;
			case '1':
				var = "一";
				break;
			case '2':
				var = "二";
				break;
			case '3':
				var = "三";
				break;
			case '4':
				var = "四";
				break;
			case '5':
				var = "五";
				break;
			case '6':
				var = "六";
				break;
			case '7':
				var = "七";
				break;
			case '8':
				var = "八";
				break;
			case '9':
				var = "九";
				break;
			default:
		}

		return var;
	}


	public  static  String getChineseDate(Date date){
		String chineseDate = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String datestr = sdf.format(date);
		System.out.println(datestr);
		String[] strs = datestr.split("-");
		// 年
		for (int i = 0; i < strs[0].length(); i++) {
			chineseDate += formatDigit(strs[0].charAt(i));
		}
		// chineseDate = chineseDate+"年";
		// 月
		char c1 = strs[1].charAt(0);
		char c2 = strs[1].charAt(1);
		String newmonth = "";
		if (c1 == '0') {
			newmonth = String.valueOf(formatDigit(c2));
		} else if (c1 == '1' && c2 == '0') {
			newmonth = "十";
		} else if (c1 == '1' && c2 != '0') {
			newmonth = "十" + formatDigit(c2);
		}
		chineseDate = chineseDate + "年" + newmonth + "月";
		// 日
		char d1 = strs[2].charAt(0);
		char d2 = strs[2].charAt(1);
		String newday = "";
		if (d1 == '0') {//单位数天
			newday = String.valueOf(formatDigit(d2));
		} else if (d1 != '1' && d2 == '0') {//几十
			newday = String.valueOf(formatDigit(d1)) + "十";
		} else if (d1 != '1' && d2 != '0') {//几十几
			newday = formatDigit(d1) + "十" + formatDigit(d2);
		} else if (d1 == '1' && d2 != '0') {//十几
			newday = "十" + formatDigit(d2);
		} else {//10
			newday = "十";
		}
		chineseDate = chineseDate + newday + "日";

		return chineseDate;
	}


	/**
	 * @title: StringUtils
	 * @projectName com.chenpeng.cpeducloud.util
	 * @description: TODO 当前月下个月是第几季度
	 */
	public static String getQuarterByDate() {

		Calendar calendar = Calendar.getInstance();

		String year = String.valueOf(calendar.get(Calendar.YEAR));
		//calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)+1);
		calendar.add(Calendar.MONTH,1);

		int mouth = calendar.get(Calendar.MONTH ) + 1;
		if(mouth>=1 && mouth<=3){
			return year + "01";
		}else if(mouth>=4 && mouth<=6){
			return year + "02";
		}else if(mouth>=7 && mouth<=9){
			return year + "03";
		}else if(mouth>=10 && mouth<=12){
			return year + "04";
		}else{
			return "00";
		}
	}



	/**
	 * @title: StringUtils
	 * @projectName com.chenpeng.cpeducloud.util
	 * @description: TODO 当前月是第几季度
	 */
	public static String getNowQuarterByDate() {

		Calendar calendar = Calendar.getInstance();

		String year = String.valueOf(calendar.get(Calendar.YEAR));
		//calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)+1);

		int mouth = calendar.get(Calendar.MONTH ) + 1;
		if(mouth>=1 && mouth<=3){
			return year + "01";
		}else if(mouth>=4 && mouth<=6){
			return year + "02";
		}else if(mouth>=7 && mouth<=9){
			return year + "03";
		}else if(mouth>=10 && mouth<=12){
			return year + "04";
		}else{
			return "00";
		}
	}


	public static int getAge(Date birthDay) {
		Calendar cal = Calendar.getInstance();
		if (cal.before(birthDay)) { //出生日期晚于当前时间，无法计算
			throw new IllegalArgumentException(
					"The birthDay is before Now.It's unbelievable!");
		}
		int yearNow = cal.get(Calendar.YEAR);  //当前年份
		int monthNow = cal.get(Calendar.MONTH);  //当前月份
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); //当前日期
		cal.setTime(birthDay);
		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH);
		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
		int age = yearNow - yearBirth;   //计算整岁数
		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {
				if(dayOfMonthNow < dayOfMonthBirth){
					age--;//当前日期在生日之前，年龄减一
				}
			} else {
				age--;//当前月份在生日之前，年龄减一
			}
		} return age;
	}

	/**
	 * 校验时间戳格式
	 */
	public static boolean checkTimeStampRegax(String time) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			@SuppressWarnings("unused")
			Date t = dateFormat.parse(time);
		}
		catch (Exception ex) {
			return false;
		}
		return true;
	}

}
